//
//  WBVisitorView.swift
//  迷你微博
//
//  Created by Json on 2020/2/27.
//  Copyright © 2020 Json. All rights reserved.
//

import UIKit

// 访客视图
class WBVisitorView: UIView {
    
    /*
     访客视图信息字典 [imageName / message]
     如果是首页 imageName == ""
     如果为nil需要解包
     imageName == nil
     var visitorInfo: [String: String?]? {
     */
    var visitorInfo: [String: String]? {
        didSet {
            // 取字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                return
            }
            // 设置信息
            tipLabel.text = message
            // 设置图像，如果是首页不需设置
            if imageName == "" {
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            // 其他控制器的访客视图不需要小房子/遮罩
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 旋转图标动画（首页）
    private func startAnimation(){
        /**CABasicAnimation：
         duration    动画的时间
         repeatCount    重复的次数。不停重复设置为 HUGE_VALF
         repeatDuration    设置动画的时间。
         beginTime    指定动画开始的时间。从开始延迟几秒的话，设置为【CACurrentMediaTime() + 秒数】 的方式
         timingFunction    设置动画的速度变化
         autoreverses    动画结束时是否执行逆动画
         fromValue    所改变属性的起始值
         toValue    所改变属性的结束时的值
         byValue    所改变属性相同起始值的改变量
         */
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        // 动画完成之后不删除，如果 iconView 被释放，动画会一起销毁
        // 在设置连续播放动画非常有用
        anim.isRemovedOnCompletion = false
        
        // 将动画添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    // 私有控件
    // 懒加载属性只有调用 UIKit 控件的指定函数不需指定类型，其他的都需要指定类型
    // 图像视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 小房子
    private lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 提示标签
    private lazy var tipLabel: UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    // 注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black,backgroundImageName: "common_button_white_disable" )
    // 登陆按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton("登陆", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black,backgroundImageName: "common_button_white_disable")
}

//设置界面
extension WBVisitorView {
    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 文本居中
        tipLabel.textAlignment = .center
        
        // 2.取消Autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 3.自动布局
//        addConstraint(NSLayoutConstraint(
//            item: 视图,
//            attribute: 约束属性,
//            relatedBy: 约束关系,
//            toItem: 参照视图,
//            attribute: 参照属性,
//            multiplier: 乘积,
//            constant: 约束数值))
        // 图像
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -60))
        // 小房子
        addConstraint(NSLayoutConstraint(
            item: houseIconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: houseIconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -8))
        // 提示标签
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .top,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20.0))
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 236))
        // 注册按钮
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .left,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .left,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20.0))
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        // 登陆按钮
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .right,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .right,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20.0))
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: registerButton,
            attribute: .width,
            multiplier: 1.0,
            constant: 0))
        // 遮罩图像，使用 VFL 方式
        // views: 定义 VFL 中的控件名称与实际名称的映射关系
        let viewDict = [
            "maskIconView":maskIconView,
            "registerButton":registerButton
        ]
        // metrics: 定义 VFL 中（）指定的常熟映射关系
        let metrics = ["spacing":-20]
        addConstraints(NSLayoutConstraint.constraints(
            // VFL 公式，水平布局
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            // 数组: [.alignAllCenterX | .alignAllCenterY],
            options: [],
            metrics: nil,
            views: viewDict))
        addConstraints(NSLayoutConstraint.constraints(
            //withVisualFormat: "V:|-0-[maskIconView]-(-33)-[registerButton]",
            //metrics: nil,
            withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerButton]",
            options: [],
            metrics: metrics,
            views: viewDict))
    }
}
